package servlet;

import dao.Bus_userDao;
import dao.UserDao;
import dao.impl.Bus_userDaoImpl;
import dao.impl.UserDaoImpl;
import pojo.Bus_user;
import pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@WebServlet(urlPatterns = "/login.do")
public class loginServlet extends HttpServlet {
    private static UserDao userDao=new UserDaoImpl();
    private static Bus_userDao bus_userDao=new Bus_userDaoImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user_type = req.getParameter("user_type");
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        System.out.println("username"+username+"pwd"+pwd);
//        req.getRequestDispatcher("/WEB-INF/userPage.html").forward(req,resp);
        boolean check;
        switch (user_type){
//            顾客
            case "0":
                check=checkCustomer(username,pwd,req);
                if (check){
                    req.getRequestDispatcher("/userBase.jsp").forward(req,resp);
                }else{
                    PrintWriter out = resp.getWriter();
                    resp.setContentType("text/html;charset=gb2312");
                    out.print("<script>alert('Login Fail');window.location.href='/DataBaseProject_war_exploded/login.html'</script>");
                }
                 break;
//            骑手
            case "1":

                break;
//            商家
            case "2":
                check=checkBus_user(username,pwd);
                if (check){
                    req.getRequestDispatcher("/bus_OrderViewServlet.do").forward(req,resp);
                }else{
                    PrintWriter out = resp.getWriter();
                    resp.setContentType("text/html;charset=gb2312");
                    out.print("<script>alert('Login Fail');window.location.href='/DataBaseProject_war_exploded/login.html'</script>");
                }
                break;

        }
    }

//    需要顺便将user的相关信息放入Context和Session中，方便后续取数据
    private boolean checkCustomer(String username,String pwd,HttpServletRequest req){
        List<User> userList=userDao.findAll();
        for (User user :
                userList) {
            if ((user.getU_name().equals(username)) && (user.getU_pwd().equals(pwd))){
                HttpSession session=req.getSession();
                session.setAttribute("username",user.getU_name());
                getServletContext().setAttribute("u_no",user.getUno());
                System.out.println("username"+user.getU_name()+"pwd"+pwd+"u_no"+user.getUno());
                return true;
            }
        }
        return false;
    }


    private boolean checkBus_user(String username,String pwd){
        List<Bus_user> bus_userList=bus_userDao.findBus_userBybus_username(username);
        Bus_user bus_user=bus_userList.get(0);
        if (bus_user==null)
            return false;
        if (bus_user.getBus_username().equals(username) && bus_user.getBus_pwd().equals(pwd))
            return true;
        return false;
    }
}
